<?php
/**
 * 抢购活动管理
 */
namespace Admin\Controller;
use Think\Controller;
use Think\Log;
class PExchangeController extends Controller
{
    protected $model;
    protected $return;
   
    public function __construct(){
        header("Access-Control-Allow-Origin: *");
        $this->model = D('PExchange');
        $this->return = array('errcode'=>0, 'errmsg'=>'ok');
        $admin = session('rush_admin');
        if (!$admin) {
            $this->ajaxReturn(array('errcode'=>10000,'errmsg'=>'请先登录'));
        }
    }

    public function index()
    {
        $data['name'] = I('post.name', '', 'trim');
        $data['type'] = I('post.type', 0, 'intval');
        $data['page'] = I('post.page', 1, 'intval');
        $data['page_size'] = I('page_size', 10, 'intval');
        //Log::write(var_export($data, true), 'data');
        $res = $this->model->getList($data);

        if ($res) {
            $this->ajaxReturn(array_merge($this->return, $res));
        }else{
            $this->ajaxReturn($this->model->getErrMsg());
        }
    }


    public function add()
    {
        $data['name'] = I('post.name', '', 'trim');
        $data['url'] = I('post.url', '', 'trim');
        $data['score'] = I('post.score', 0, 'intval');
        $data['total'] = I('post.total', 0, 'intval');
        $data['type'] = I('post.type', 0, 'intval');
        $data['link'] = I('post.link', '', 'trim');
        $data['is_show'] = I('post.is_show', '', 'trim');
        $data['material'] = I('post.material', '', 'trim');
        $data['desc'] = I('post.desc', '', 'trim');

        if (empty($data['name'])) {
            $this->ajaxReturn(array('errcode'=>10001, 'errmsg'=>'礼品名称不能为空'));
        }
        if ($data['type'] <= 0) {
            $this->ajaxReturn(array('errcode'=>10002, 'errmsg'=>'请选择礼品类型'));
        }
        if (empty($data['url'])) {
            $this->ajaxReturn(array('errcode'=>10003, 'errmsg'=>'礼品图片不能为空'));
        }
        if ($data['score'] <= 0) {
            $this->ajaxReturn(array('errcode'=>10004, 'errmsg'=>'兑换积分必须为正整数'));
        }
        if ($data['total'] <= 0) {
            $this->ajaxReturn(array('errcode'=>10005, 'errmsg'=>'礼品库存必须为正整数'));
        }
        $map['name'] = $data['name'];
        $map['is_deleted'] = 0;
        $count = $this->model->where($map)->count();
        if ($count) {
            $this->ajaxReturn(array('errcode'=>20001, 'errmsg'=>'礼品名称已存在'));
        }
        $data['sku'] = $data['total'];
        $data['add_time'] = time();
        $res = $this->model->add($data);
        if ($res) {
            $this->ajaxReturn(array('errcode'=>0, 'errmsg'=>'新增礼品成功'));
        }else{
            $this->ajaxReturn(array('errcode'=>50000, 'errmsg'=>'新增礼品失败'));
        }
    }
        
    public function edit()
    {
        $id = I('post.id', 0, 'intval');
        $res = $this->model->find($id);
        if ($res) {
            $this->ajaxReturn(array('errcode'=>0, 'errmsg'=>'ok', 'data'=>$res));
        }else{
            $this->ajaxReturn(array('errcode'=>50000, 'errmsg'=>'查找不到对应的礼品数据'));
        }
    }

    public function update()
    {
        $data['name'] = I('post.name', '', 'trim');
        $data['url'] = I('post.url', '', 'trim');
        $data['score'] = I('post.score', 0, 'intval');
        $data['total'] = I('post.total', 0, 'intval');
        $data['type'] = I('post.type', 0, 'intval');
        $data['id'] = I('post.id', 0, 'intval');
        $data['link'] = I('post.link', '', 'trim');
        $data['is_show'] = I('post.is_show', '', 'trim');
        $data['material'] = I('post.material', '', 'trim');
        $data['desc'] = I('post.desc', '', 'trim');
        $map['id'] = I('post.id', 0, 'intval');
        $act = M("PExchange")->where($map)->find();
        if ($act and $act['sku'] > $data['total']) {
             $this->ajaxReturn(array('errcode'=>10006, 'errmsg'=>'总库存不能小于剩余数量'));
        }
        if (empty($data['name'])) {
            $this->ajaxReturn(array('errcode'=>10001, 'errmsg'=>'礼品名称不能为空'));
        }
        if ($data['type'] <= 0) {
            $this->ajaxReturn(array('errcode'=>10002, 'errmsg'=>'请选择礼品类型'));
        }
        if (empty($data['url'])) {
            $this->ajaxReturn(array('errcode'=>10003, 'errmsg'=>'礼品图片不能为空'));
        }
        if ($data['score'] <= 0) {
            $this->ajaxReturn(array('errcode'=>10004, 'errmsg'=>'兑换积分必须为正整数'));
        }
        if ($data['total'] <= 0) {
            $this->ajaxReturn(array('errcode'=>10005, 'errmsg'=>'礼品库存必须为正整数'));
        }
        $map['id'] = $data['id'];
        $map['is_deleted'] = 0;
        $gift = $this->model->where($map)->find();
        if (!$gift) {
            $this->ajaxReturn(array('errcode'=>20001,'errmsg'=>'要修改的礼品不存在'));
        }
        unset($map);
        $map['name'] = $data['name'];
        $map['is_deleted'] = 0;
        $gift = $this->model->where($map)->find();
        if ($gift and $gift['id'] != $data['id']) {
            $this->ajaxReturn(array('errcode'=>20001, 'errmsg'=>'礼品名称已存在'));
        }
        $data['sku'] = $data['total'];
        //$data['add_time'] = time();
        $res = $this->model->save($data);
        if ($res !== false) {
            $this->ajaxReturn(array('errcode'=>0, 'errmsg'=>'修改礼品成功'));
        }else{
            $this->ajaxReturn(array('errcode'=>50000, 'errmsg'=>'修改礼品失败'));
        }
    }


    public function record()
    {
        $data['phone'] = I('post.phone', '', 'trim');
        $data['account'] = I('post.account', '', 'trim');
        $data['keyword'] = I('keyword', '', 'trim');
        $data['name'] = I('post.name', '', 'trim');
        $data['start'] = I('post.start', '', 'trim');
        $data['end'] = I('post.end', '', 'trim');

        $res = $this->model->getRecordList($data);

        if ($res) {
            $this->ajaxReturn(array_merge($this->return, $res));
        }else{
            $this->ajaxReturn($this->model->getErrMsg());
        }

    }

    public function top(){
        $map['id'] = I('post.id', 0, 'intval');
        $act = M("PExchange")->where($map)->find();
        unset($map);
        if($act){

            $max=1;
            $p=M("PExchange")->order('top desc')->find();

            if($p)
                $max=$p['top']+1;
            $act['top']=$max;
            $res = $this->model->save($act);
            if ($res !== false) {
                $this->ajaxReturn(array('errcode'=>0, 'errmsg'=>'修改礼品成功'));
            }else{
                $this->ajaxReturn(array('errcode'=>50000, 'errmsg'=>'修改礼品失败'));
            }
        }
        else{
            $this->ajaxReturn(array('errcode'=>50000, 'errmsg'=>'没有该礼品存在'));
        }
    }

    public function del()
    {
        $id = I('post.id', 0, 'intval');
        $data['is_deleted'] = 1;
        $res = M("PExchange")->where(array('id'=>$id))->save($data);
        if ($res) {
            $this->ajaxReturn($this->return);
        }else{
            $this->ajaxReturn(array('errcode'=>50000, 'errmsg'=>'删除失败'));
        }
    }
    

}